// pages/role-register/index.js
import api from '../../utils/api.js';
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    roleId: "0",//即roleId
    userId:'',
    rcId:'',
    rc: {
      "address": "大别山",
      "name": "铁柱合作社",
      "land": "10",
      "pond": "12",
      "forest": "16",
      "head": "https://dcdn.it120.cc/2021/07/13/28e43e17-04ff-410b-a2ec-4c2a2b21d5fa.png",
      "description": "热烈庆祝建党一百周年",
      "farmerNumber": 1,
      "swiper1": null,
      "swiper2": null,
      "swiper3": null,
      "descriptionImage": null
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     this.setData({userId:app.globalData.userId})
      wx.getStorage({
        key:"roleId",
        success:(res)=>{
          console.log("获取roleId")
          console.log(res.data)
          this.setData({roleId:res.data})
        }
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onChange(event) {
    this.setData({
      radio: event.detail,
    });
  },

  onClick(event) {
    const { name } = event.currentTarget.dataset;
    this.setData({
      radio: name,
    });
  },

  onRCInputChange(event) {
    // event.detail 为当前输入的值
    console.log(event.detail);
  },

  onAdminUserInputChange(event) {
    // event.detail 为当前输入的值
    console.log(event.detail);
  },

  onAdminPwdInputChange(event) {
    // event.detail 为当前输入的值
    console.log(event.detail);
  },

  onInput(e){
    console.log(e)
    console.log(e.currentTarget.id)
    this.setData({[e.currentTarget.id]: e.detail})
    console.log(this.data.item)
  },

  onSubmit(){
    if(this.data.roleId==1){//申请农户
      const url = '/users/'+app.globalData.userId+'/role?role='+this.data.roleId
      console.log(this.data.radio)
      api.put(url,{
        data:''
      })
      wx.navigateBack({
        delta: 0,
      })
    }else if(this.data.roleId==2){//注册合作社
      const url = '/RC/users/'+app.globalData.userId
      api.post(url,this.data.rc)
        .then(res=>{
          console.log(res)
        })
    }
    wx.navigateBack({
      delta: 0,
    })
  }
})